package com.mt.controller;


import cn.hutool.core.collection.CollUtil;
import com.mt.VO.SelectSeatVO;
import com.mt.constants.Result;
import com.mt.enums.Status;
import com.mt.pojo.CinemaSchedule;
import com.mt.service.CinemaScheduleService;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Wang
 * @since 2020-12-11
 */
@RestController
@RequestMapping("/schedule")
@Log4j2
public class CinemaScheduleController {

    @Resource
    CinemaScheduleService scheduleService;

    @PostMapping("/batchInsert")
    public Result batchInsertSchedule(List<CinemaSchedule> scheduleList){
        Result<Object> result = new Result<>();
        List<CinemaSchedule> conflictList = scheduleService.batchInsertSchedule(scheduleList);
        if (! conflictList.isEmpty()){
            result.setData(conflictList);
            result.setCode(Status.SCHDULE_HAVE_CONFLICT.getCode());
            result.setMsg(Status.SCHDULE_HAVE_CONFLICT.getMsg());
        }else {
            result.success();
        }
        return result;
    }
    @PostMapping("/add")
    public Result batchInsertSchedule(@RequestBody CinemaSchedule schedule){
        Result<Object> result = new Result<>();
        ArrayList<CinemaSchedule> list = CollUtil.newArrayList();
        list.add(schedule);
        List<CinemaSchedule> conflictList = scheduleService.batchInsertSchedule(list);
        if (! conflictList.isEmpty()){
            log.error(conflictList);
            result.setData(conflictList);
            result.setCode(Status.SCHDULE_HAVE_CONFLICT.getCode());
            result.setMsg(Status.SCHDULE_HAVE_CONFLICT.getMsg());
        }else {
            result.success();
        }
        return result;
    }

    @GetMapping("/detail")
    public Result getScheduleDetailInfo(@RequestParam("scheduleId") int scheduleId){
        Result<Object> result = new Result<>();
        result.success(scheduleService.getScheduleDetailInfo(scheduleId));
        return result;
    }

    @GetMapping("/{scheduleId}")
    public Result getScheduleSeatsDetail(@PathVariable("scheduleId") int scheduleId){
        Result<Object> result = new Result<>();
        SelectSeatVO seatsDetail = scheduleService.getScheduleSeatsDetail(scheduleId);
        result.success(seatsDetail);
        return result;
    }

    @GetMapping("/list")
    public Result listScheduleByFilm(int filmId,int cinemaId, String queryTime){
        Result<Object> result = new Result<>();
        result.success(scheduleService.listScheduleByFilm(filmId,cinemaId,queryTime));
        return  result;
    }

    //查询当前电影院的排片情况
    @GetMapping("/page")
    public Result pageScheduleByCinema(@RequestParam("currentPage") int currentPage,
                                       @RequestParam("pageSize") int pageSize,
                                        @RequestParam("cinemaId")int cinemaId,
                                       @RequestParam("filmName") String filmName){
        Result<Object> result = new Result<>();
        result.success(scheduleService.pageScheduleByCinema(currentPage, pageSize,cinemaId,filmName));
        return  result;
    }

    @PostMapping("/delete")
    public Result deleteSchedule (@RequestBody CinemaSchedule schedule){
        Result<Object> result = new Result<>();
        result.success(scheduleService.deleteSchedule(schedule.getId()));
        return  result;
    }

}
